package com.lin.manager.common.resp;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel("响应实体")
@Data
public class R<T> {

    @ApiModelProperty("响应码，200为成功")
    private Integer code;
    @ApiModelProperty("失败时的具体失败信息")
    private String msg;
    @ApiModelProperty("响应的具体对象")
    private T data;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> R<T> ok() {
        return ok(RespCode.SUCCESS.getCode(), RespCode.SUCCESS.getMsg(), null);
    }

    public static <T> R<T> ok(T data) {
        return ok(RespCode.SUCCESS.getCode(), RespCode.SUCCESS.getMsg(), data);
    }

    public static <T> R<T> ok(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> fail() {
        return ok(RespCode.FAIL.getCode(), RespCode.FAIL.getMsg(), null);
    }

    public static <T> R<T> fail(T data) {
        return fail(RespCode.FAIL.getCode(), RespCode.FAIL.getMsg(), data);
    }

    public static <T> R<T> fail(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> fail(RespCode respCode, T data) {
        return fail(respCode.getCode(), respCode.getMsg(), data);
    }
}